//
//  HTClassPersonalInforVC.swift
//  QuitSmokingApp
//  
//  Created by wealon on 2024.
//  QuitSmokingApp.
//  


import UIKit
import BRPickerView

class HTClassPersonalInforVC: HTClassViewController {
    var var_p_info = HTClassStorage.ht_getPersonalInfor()
    lazy var var_nameView: HTClassTextFieldView = {
        let var_view = HTClassTextFieldView(frame: .zero)
        var_view.var_titleLabel.text = "Name"
        var_view.var_textField.text = var_p_info?.var_name
        return var_view
    }()
    var var_genderIndex = 0
    lazy var var_genderView: HTClassTextFieldView = {
        let var_view = HTClassTextFieldView(frame: .zero)
        var_view.var_titleLabel.text = "Gender"
        var_view.var_textField.text = var_p_info?.var_gender
        var_view.BLOCK_tapBLOCK = {
            self.view.endEditing(true)
            let var_style = BRPickerStyle()
            var_style.maskColor = .clear
            var_style.titleLineColor = UIColor("#CAD6E9")
            var_style.cancelTextColor = UIColor("#FF0B0B")
            var_style.cancelTextFont = .systemFont(ofSize: 18, weight: .semibold)
            var_style.cancelBtnTitle = "Cancel"
            
            var_style.doneTextColor = UIColor("#258BC4")
            var_style.doneBtnTitle = "OK"
            var_style.doneTextFont = .systemFont(ofSize: 18, weight: .semibold)
            var_style.pickerColor = .white
            var_style.separatorColor = UIColor("#E6E6E6")
            var_style.pickerTextColor = UIColor("#999999")
            var_style.pickerTextFont = .systemFont(ofSize: 14, weight: .medium)
            var_style.selectRowTextColor = UIColor.black
            
            
            let var_picke = BRStringPickerView()
            var_picke.pickerStyle = var_style
            var_picke.pickerMode = .componentSingle
            var_picke.title = ""
            var_picke.dataSourceArr = ["male", "female"]
            var_picke.selectIndex = self.var_genderIndex
            var_picke.resultModelBlock = { var_resmodel in
                self.var_genderIndex = var_resmodel?.index ?? 0
                self.var_genderView.var_textField.text = var_resmodel?.value ?? ""
            }
            var_picke.show()
            
        }
        return var_view
    }()
    var var_dateBirth: Date? = Date()
    lazy var var_dateBirthView: HTClassTextFieldView = {
        let var_view = HTClassTextFieldView(frame: .zero)
        var_view.var_titleLabel.text = "Date of birth"
        var_view.var_textField.text = var_p_info?.var_dateBirth
        var_view.BLOCK_tapBLOCK = { [weak self] in
            guard let self = self else { return }
            self.view.endEditing(true)
            let var_datePickerView = BRDatePickerView()
            var_datePickerView.pickerMode = .YMD
            var_datePickerView.selectDate = self.var_dateBirth
            var_datePickerView.isAutoSelect = false
            var_datePickerView.showUnitType = .none
            var_datePickerView.maxDate = Date()
            var_datePickerView.resultBlock = { var_selectDate, var_selectValue in
                self.var_dateBirth = var_selectDate
                var_view.var_textField.text = var_selectValue
            }
            let var_style = BRPickerStyle()
            var_style.maskColor = .clear
            var_style.titleLineColor = UIColor("#CAD6E9")
            var_style.cancelTextColor = UIColor("#FF0B0B")
            var_style.cancelTextFont = .systemFont(ofSize: 18, weight: .semibold)
            var_style.cancelBtnTitle = "Cancel"
            var_style.doneTextColor = UIColor("#258BC4")
            var_style.doneBtnTitle = "OK"
            var_style.doneTextFont = .systemFont(ofSize: 18, weight: .semibold)
            
            var_style.selectRowTextColor = .black
            var_style.pickerTextColor = UIColor("#999999")
            var_datePickerView.pickerStyle = var_style
            var_datePickerView.show()
        }
        return var_view
    }()
    
    lazy var var_heightView: HTClassTextFieldView = {
        let var_view = HTClassTextFieldView(frame: .zero)
        var_view.var_textField.keyboardType = .decimalPad
        var_view.var_titleLabel.text = "Height"
        var_view.var_unitLabel.text = "cm"
        var_view.var_textField.text = var_p_info?.var_height
        return var_view
    }()
    lazy var var_weightView: HTClassTextFieldView = {
        let var_view = HTClassTextFieldView(frame: .zero)
        var_view.var_textField.keyboardType = .decimalPad
        var_view.var_titleLabel.text = "Weight"
        var_view.var_unitLabel.text = "kg"
        var_view.var_isLine = false
        var_view.var_textField.text = var_p_info?.var_weight
        return var_view
    }()
    
    lazy var var_bgView: UIView = {
        let var_view = UIView()
        var_view.backgroundColor = .black
        return var_view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Edit personal information"

    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func ht_setUI() {
        super.ht_setUI()
        view.addSubview(var_bgView)
        var_bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(HTClassTool.STATIC_navigationBarTotalHeight)
        }
        
        var_bgView.addSubview(self.var_nameView)
        var_nameView.snp.makeConstraints { make in
            make.right.left.equalToSuperview()
            make.height.equalTo(48.auto())
            make.top.equalToSuperview().offset(20.auto())
        }
        
        var_bgView.addSubview(self.var_genderView)
        var_genderView.snp.makeConstraints { make in
            make.right.left.equalToSuperview()
            make.height.equalTo(48.auto())
            make.top.equalTo(var_nameView.snp.bottom)
        }
        
        var_bgView.addSubview(self.var_dateBirthView)
        var_dateBirthView.snp.makeConstraints { make in
            make.right.left.equalToSuperview()
            make.height.equalTo(48.auto())
            make.top.equalTo(var_genderView.snp.bottom)
        }
        
        var_bgView.addSubview(self.var_heightView)
        var_heightView.snp.makeConstraints { make in
            make.right.left.equalToSuperview()
            make.height.equalTo(48.auto())
            make.top.equalTo(var_dateBirthView.snp.bottom)
        }
        
        var_bgView.addSubview(self.var_weightView)
        var_weightView.snp.makeConstraints { make in
            make.right.left.equalToSuperview()
            make.height.equalTo(48.auto())
            make.top.equalTo(var_heightView.snp.bottom)
        }
        
        let var_saveBtn = UIButton()
        var_saveBtn.setTitle("Save", for: .normal)
        var_saveBtn.setTitleColor(.white, for: .normal)
        var_saveBtn.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
        var_saveBtn.layer.cornerRadius = 10
        var_saveBtn.layer.masksToBounds = true
        var_saveBtn.onTap { [weak self] in
            self?.ht_savePersonalInfor()
        }
        var_bgView.addSubview(var_saveBtn)
        var_saveBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-100)
            make.centerX.equalToSuperview()
            make.width.equalTo(198)
            make.height.equalTo(44)
        }
        var_saveBtn.layoutIfNeeded()
        var_saveBtn.ht_setVerticalGradient(startColor: UIColor("#5CEBFD"), endColor: UIColor("#0691F3"))
    }
    
    func ht_savePersonalInfor() {
        guard let var_name = self.var_nameView.var_textField.text else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter a name", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        if var_name.count == 0 {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter a name", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            return
        }
        guard let var_gender = self.var_genderView.var_textField.text else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please select a gender", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        if var_gender.count == 0 {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please select a gender", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            return
        }
        guard let var_dateBirth = self.var_dateBirthView.var_textField.text else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please select the date of birth", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        if var_dateBirth.count == 0 {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please select the date of birth", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            return
        }
        guard let var_height = self.var_heightView.var_textField.text else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter height", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        if var_height.count == 0 {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter height", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            return
        }
        guard let _ = Double(var_height) else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter the correct height", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        guard let var_weight = self.var_weightView.var_textField.text else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter your weight", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        if var_weight.count == 0 {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter your weight", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            return
        }
        guard let _ = Double(var_weight) else { return HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Please enter the correct weight", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime) }
        var var_model = HTClassPersonalInforModel(var_name: var_name)
        var_model.var_gender = var_gender
        var_model.var_dateBirth = var_dateBirth
        var_model.var_height = var_height
        var_model.var_weight = var_weight
        HTClassStorage.ht_savePersonalInfor(var_model)
        HTClassStorage.ht_saveLogin(true)
        if HTClassStorage.ht_getLogin() {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Personal information modified successfully", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            self.navigationController?.popViewController(animated: true)
        } else {
            HTClassHudView().ht_showToastOnView(var_view: self.view, var_title: "Account created successfully", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
            self.navigationController?.popViewController(animated: true)
        }
       
        
    }
}

